<?php
/*
	通用控制器
*/
namespace Admin\Controller;
use Think\Controller;
class CommonController extends AdminbaseController {
    
        protected $oss_sdk_service; 

        public function _initialize(){
	
                parent::_initialize();
                import('@.Util.sdk');

                $this->oss_sdk_service = new \ALIOSS();

                //设置是否打开curl调试模式
                $this->oss_sdk_service->set_debug_mode(FALSE);
	}
	
        function uploadImageToOss($filePath, $fileName) {
            $bucket = C('BUCKET_NAME');
            $file = $filePath;
            $object = $fileName;  //object名称
            $response_upload_file_by_file = $this->oss_sdk_service->upload_file_by_file($bucket,$object,$file);

            return $response_upload_file_by_file;
        }
        
        function getImageUrlFromOss($object) {
            $bucket = C('BUCKET_NAME'); 
            $get_object_url = $this->oss_sdk_service->get_object($bucket, $object); 
            return $get_object_url->header['_info']['url'];
        }
        
        function ueditorUpload() {
            $data = new \Org\Util\Ueditor();
            echo $data->output();
        }
        
        function downloadFile() {
            
            $filePath = I('get.filePath');
        
            $fileName = I('get.fileName');
            
            if (!file_exists($filePath)) {   //检查文件是否存在  
                echo "文件找不到";
                exit;
            } else {
                $file = fopen($filePath, "r"); // 打开文件
                // 输入文件标签
                Header("Content-type: application/octet-stream");
                Header("Accept-Ranges: bytes");
                Header("Accept-Length: " . filesize($filePath));
                Header("Content-Disposition: attachment; filename=" . $fileName);
                // 输出文件内容
                echo fread($file, filesize($filePath));
                fclose($file);
                exit();
            }
        }
        function uploadFile() {
            
            $targetFolder = '/Uploads/files'; // Relative to the root

            if (!empty($_FILES)) {
                    
                $tempFile = $_FILES['Filedata']['tmp_name'];
                $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;

                $fileParts = pathinfo($_FILES['Filedata']['name']);

                $object = md5(time()) . '.'. $fileParts['extension'];

                $targetFile = rtrim($targetPath,'/') . '/' . $object;

                move_uploaded_file($tempFile,$targetFile);

                $data['url'] = $targetFolder. '/' . $object;
                $data['file'] = $targetFile;
                $data['extension'] = $fileParts['extension'];
                
                echo json_encode($data);
                    
            }
        }
        
        function deleteLocalFile() {
            $file = I('post.file');
            unlink($file);
        }
        
        function uploadImage() {
            
            $targetFolder = '/Uploads'; // Relative to the root

            $verifyToken = md5('unique_salt' . $_POST['timestamp']);

            if (!empty($_FILES)) {
                    
                    $tempFile = $_FILES['Filedata']['tmp_name'];
                    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
                 
                    // Validate the file type
                    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
                    $fileParts = pathinfo($_FILES['Filedata']['name']);

                    $object = md5(time()) . '.'. $fileParts['extension'];
                    
                    $targetFile = rtrim($targetPath,'/') . '/' . $object;
                    
                    if (in_array($fileParts['extension'],$fileTypes)) {
                            move_uploaded_file($tempFile,$targetFile);
                            $this->uploadImageToOss($targetFile, $object);
                            unlink($targetFile);
                            echo $this->getImageUrlFromOss($object);
                    } else {
                            echo 'Invalid file type.';
                    }
            }
        }
        
}